/**  
 * All rights Reserved, Designed By www.shatayy.com
 * @Title:  ${file_name}   
 * @Package ${package_name}   
 * @Description:    ${todo}(用一句话描述该文件做什么)   
 * @author: 宁波沙塔信息技术有限公司     
 * @date:   ${date} ${time}   
 * @version V1.0 
 * @Copyright: ${year} www.tydic.com Inc. All rights reserved. 
 * 注意：本内容仅限于宁波沙塔信息技术股份有限公司内部传阅，禁止外泄以及用于其他的商业目
 */  
package com.nbst.comnutil;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;

import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.util.StringUtils;

import com.alibaba.fastjson.JSONObject;
import com.jayway.jsonpath.JsonPath;
import com.nbst.model.base.Menu;

public class JsonUtil {

	@SuppressWarnings("unchecked")
	public static Map<String, Object> json2Map(String json)
			throws JsonParseException, JsonMappingException, IOException {
		ObjectMapper mapper = new ObjectMapper();
		Map<String, Object> map = mapper.readValue(json, Map.class);
		return map;
	}
	
	@SuppressWarnings("unchecked")
	public static Map<String, String> toMap(String json)
			throws JsonParseException, JsonMappingException, IOException {
		ObjectMapper mapper = new ObjectMapper();
		Map<String, String> map = mapper.readValue(json, Map.class);
		return map;
	}

	public static String pojoToJson(Object obj) throws JsonGenerationException,
			JsonMappingException, IOException {
		ObjectMapper mapper = new ObjectMapper();
		String json = mapper.writeValueAsString(obj);
		return json;
	}

	public static <T> T json2Pojo(String json, Class<T> toParseClass)
			throws JsonParseException, JsonMappingException, IOException {
		if (json == null) {
			return null;
		}
		ObjectMapper mapper = new ObjectMapper();
		return mapper.readValue(json, toParseClass);
	}

	public static String deelMenu(List<Menu> menuList) {
//		JSONObject jsonO = new JSONObject();
		JSONObject jsonB = new JSONObject();
		JSONObject json = new JSONObject();
		JSONArray jsonNode = new JSONArray();
		JSONArray jsonLeaf = new JSONArray();
		for (Menu menu : menuList) {
			if (menu.getpPid() == null) {
				jsonB.put("id", menu.getpId());
				jsonB.put("text", menu.getpName());
				jsonB.put("url", menu.getpUrl());
				String c = menu.getCheck();
				if(c == null){
					c = "";
				}
				jsonB.put("check", c);
				jsonB.put("classes", menu.getpIcon());
				jsonB.put("nodes", jsonLeaf);
				jsonNode.add(jsonB);
				jsonB.clear();
			} else {
				if(!StringUtils.isEmpty(menu.getpRid())){
					List<Map<String, String>> mp = JsonPath.read(jsonNode,
							"$.[?(@.id == '" + menu.getpRid() + "')]");
					List<Map<String, String>> mp1 = JsonPath.read(jsonNode,
							"$.[?(@.id != '" + menu.getpRid() + "')]");
					if (mp.size()>0) {
						json = (JSONObject) JSONObject.toJSON(mp.get(0));
						jsonB.put("id", menu.getpId());
						jsonB.put("text", menu.getpName());
						jsonB.put("url", menu.getpUrl());
						String c = menu.getCheck();
						if(c == null){
							c = "";
						}
						jsonB.put("check", c);
						json.getJSONArray("nodes").add(jsonB);
						jsonNode.clear();
						jsonNode.addAll(mp1);
						jsonNode.add(json);
						jsonB.clear();
					}
				}
			}
		}
//		jsonO.put("nodes", jsonNode);
		return jsonNode.toString();
	}

	public static String deelDesk(List<Menu> menuList) {
		JSONObject jsonB = new JSONObject();
		JSONArray jsonNode = new JSONArray();
		for (Menu menu : menuList) {
			jsonB.put("id", menu.getpId());
			jsonB.put("text", menu.getpName());
			jsonB.put("url", menu.getpUrl());
			jsonB.put("classes", menu.getpIcon());
			jsonNode.add(jsonB);
			jsonB.clear();
		}
		return jsonNode.toString();
	}
	
	public static String menuTree(List<Menu> menuList) {
		MenuTree mt = new MenuTree(menuList);
		return mt.buildTree();
	}
}